import {
	deleteUserConfigCustom,
	setUserConfigCustom
} from '@/api/auth/authUserConfigCustomApi.js'

//#ifdef MP-WEIXIN
const prefix = wx.getAccountInfoSync().miniProgram.appId + '_'
//#endif
//#ifndef MP-WEIXIN
const prefix = 'new_' + window.location.host + '_'
//#endif


export function localStorageGet(key) {
	return uni.getStorageSync(key)
}

export function localStorageSet(key, val) {
	if (val === null) {
		uni.removeStorageSync(key)
	} else {
		uni.setStorageSync(key, val)
	}
}

function saveToNet(key, value) {
	localStorageSet(key, value)
	if (!getToken()) {
		return
	}
	if (typeof(value) === 'undefined' || value === null) {
		deleteUserConfigCustom({
			keyName: key
		})
	} else {
		setUserConfigCustom({
			keyName: key,
			value: value
		})
	}
}

function getFromNet(key) {
	return localStorageGet(key)
}


const deptIdKey = prefix + 'deptId'

export function getDeptId() {
	return localStorageGet(deptIdKey) || -1
}

export function setDeptId(deptId) {
	return localStorageSet(deptIdKey, deptId)
}

const productIdKey = prefix + 'productId'

export function getProductId() {
	return localStorageGet(productIdKey)
}

export function setProductId(productId) {
	return localStorageSet(productIdKey, productId)
}

const showViewKey = prefix + 'showView'

export function getShowView() {
    const res= getFromNet(showViewKey)
    if (null == res) {
        return false
    }
    return res
}

export function setShowView(showView) {
    return saveToNet(showViewKey, showView)
}

const mapModelKey = prefix + 'mapModel'

export function getMapModel() {
	return getFromNet(mapModelKey) || 'point'
}

export function setMapModel(mapModel) {
	return saveToNet(mapModelKey, mapModel)
}

const openNoticeKey = prefix + 'openNotice'

export function getOpenNotice() {
	const res = getFromNet(openNoticeKey)
	if (null == res) {
	    return false
	}
	return res
}

export function setOpenNotice(openNotice) {
	return saveToNet(openNoticeKey, openNotice)
}

const latKey = prefix + 'lat'

export function getLat() {
	return localStorageGet(latKey) || 23
}

export function setLat(lat) {
	return localStorageSet(latKey, lat)
}

const lngKey = prefix + 'lng'

export function getLng() {
	return localStorageGet(lngKey) || 113
}

export function setLng(lng) {
	return localStorageSet(lngKey, lng)
}

const filterRepeatKey = prefix + 'filterRepeat'

export function getFilterRepeat() {
	const res = getFromNet(filterRepeatKey)
	if (null == res) {
	    return true
	}
	return res
}

export function setFilterRepeat(filterRepeat) {
	return saveToNet(filterRepeatKey, filterRepeat)
}

const showHiddenKey = prefix + 'showHidden'

export function getShowHidden() {
	const res =  getFromNet(showHiddenKey)
	if (null == res) {
	    return false
	}
	return res
}

export function setShowHidden(showHidden) {
	return saveToNet(showHiddenKey, showHidden)
}

const addFrameKey = prefix + 'addFrame'

export function getAddFrame() {
	const res =  getFromNet(addFrameKey)
	if (null == res) {
	    return false
	}
	return res
}

export function setAddFrame(addFrame) {
	return saveToNet(addFrameKey, addFrame)
}

const TokenKey = prefix + 'accessToken'

export function getToken() {
	return localStorageGet(TokenKey)
}

export function setToken(token) {
	localStorageSet(TokenKey, token)
}

const userNameKey = prefix + 'userName'

export function getUserName() {
	return localStorageGet(userNameKey)
}

export function setUserName(userName) {
	localStorageSet(userNameKey, userName)
}

const userIdKey = prefix + 'userId'

export function getUserId() {
	return localStorageGet(userIdKey)
}

export function setUserId(userId) {
	localStorageSet(userIdKey, userId)
}

const shortCompanyNameKey = prefix + 'shortCompanyName'

export function getShortCompanyName() {
	return localStorageGet(shortCompanyNameKey)
}

export function setShortCompanyName(shortCompanyName) {
	localStorageSet(shortCompanyNameKey, shortCompanyName)
}

const companyIdKey = prefix + 'companyId'

export function getCompanyId() {
	return localStorageGet(companyIdKey)
}

export function setCompanyId(companyId) {
	localStorageSet(companyIdKey, companyId)
}

const websiteModelKey = prefix + 'websiteModel'

export function setWebsiteModel(websiteModel) {
	return localStorageSet(websiteModelKey, websiteModel)
}

export function getWebsiteModel() {
	return localStorageGet(websiteModelKey) || 'user'
}

const languageKey = prefix + 'language'

export function getLanguage() {
	return getFromNet(languageKey)
}

export function setLanguage(language) {
	return saveToNet(languageKey, language)
}

const tabbarHeightKey = prefix + 'tabbarHeight'

export function getTabbarHeight() {
	return getFromNet(tabbarHeightKey)
}

export function setTabbarHeight(tabbarHeight) {
	return saveToNet(tabbarHeightKey, tabbarHeight)
}

const companyIconUrlKey = prefix + 'companyIconUrl'

export function getCompanyIconUrl() {
	return localStorageGet(companyIconUrlKey)
}

export function setCompanyIconUrl(companyIconUrl) {
	localStorageSet(companyIconUrlKey, companyIconUrl)
}

const userIconUrlKey = prefix + 'userIconUrl'

export function getUserIconUrl() {
	return localStorageGet(userIconUrlKey)
}

export function setUserIconUrl(userIconUrl) {
	localStorageSet(userIconUrlKey, userIconUrl)
}


const enablePhoneRegisterKey = prefix + 'enablePhoneRegister'

export function getEnablePhoneRegister() {
	return localStorageGet(enablePhoneRegisterKey) || false
}

export function setEnablePhoneRegister(enablePhoneRegister) {
	localStorageSet(enablePhoneRegisterKey, enablePhoneRegister)
}

const enableRegisterKey = prefix + 'enableRegister'

export function getEnableRegister() {
	return localStorageGet(enableRegisterKey) || false
}

export function setEnableRegister(enableRegister) {
	localStorageSet(enableRegisterKey, enableRegister)
}

const enableDemoAccountKey = prefix + 'enableDemoAccount'

export function getEnableDemoAccount() {
	return localStorageGet(enableDemoAccountKey) || false
}

export function setEnableDemoAccount(enableDemoAccount) {
	localStorageSet(enableDemoAccountKey, enableDemoAccount)
}


const openidKey = prefix + 'openid'

export function getOpenid() {
	return localStorageGet(openidKey)
}

export function setOpenid(openid) {
	localStorageSet(openidKey, openid)
}

const nickNameKey = prefix + 'nickName'

export function getNickName() {
	return localStorageGet(nickNameKey)
}

export function setNickName(nickName) {
	localStorageSet(nickNameKey, nickName)
}


const themeKey = prefix + 'theme'

export function getTheme() {
	return localStorageGet(themeKey) || 'orange'
}

export function setTheme(theme) {
	localStorageSet(themeKey, theme)
}

const userInfoKey = prefix + 'userInfo'

export function getUserInfo() {
	return localStorageGet(userInfoKey)
}

export function setUserInfo(userInfo) {
	localStorageSet(userInfoKey, userInfo)
}

const deviceNavigationComponentKey = prefix + 'deviceNavigationComponent'

export function getDeviceNavigationComponent() {
	return localStorageGet(deviceNavigationComponentKey) || 'deviceInfo'
}

export function setDeviceNavigationComponent(deviceNavigationComponent) {
	localStorageSet(deviceNavigationComponentKey, deviceNavigationComponent)
}

const statusBarKey = prefix + 'statusBar'

export function getStatusBar() {
	return localStorageGet(statusBarKey)
}

export function setStatusBar(statusBar) {
	localStorageSet(statusBarKey, statusBar)
}

const statusIndexKey = prefix + 'statusIndex'

export function getStatusIndex() {
	return localStorageGet(statusIndexKey) || 0
}

export function setStatusIndex(statusIndex) {
	localStorageSet(statusIndexKey, statusIndex)
}


const bottomBarHeightKey = prefix + 'bottomBarHeight'

export function getBottomBarHeight() {
	return localStorageGet(bottomBarHeightKey)
}

export function setBottomBarHeight(bottomBarHeight) {
	localStorageSet(bottomBarHeightKey, bottomBarHeight)
}

const mapHeightKey = prefix + 'mapHeight'

export function getMapHeight() {
	return localStorageGet(mapHeightKey)
}

export function setMapHeight(mapHeight) {
	localStorageSet(mapHeightKey, mapHeight)
}

const showChildDeviceKey = prefix + 'showChildDevice'

export function getShowChildDevice() {
	return getFromNet(showChildDeviceKey) || true
}

export function setShowChildDevice(showChildDevice) {
	return saveToNet(showChildDeviceKey, showChildDevice)
}

const enableServerLinkageKey = prefix + 'enableServerLinkage'

export function getEnableServerLinkage() {
	return localStorageGet(enableServerLinkageKey) || false
}

export function setEnableServerLinkage(enableServerLinkage) {
	localStorageSet(enableServerLinkageKey, enableServerLinkage)
}

const followKey = prefix + 'follow'

export function getFollow() {
	return localStorageGet(followKey)
}

export function setFollow(follow) {
	localStorageSet(followKey, follow)
}

const listModelKey = prefix + 'listModel'

export function getListModel() {
	return localStorageGet(listModelKey) || 'list'
}

export function setListModel(listModel) {
	localStorageSet(listModelKey, listModel)
}

const warnDescKey = prefix + 'warnDesc'

export function getWarnDesc() {
	return localStorageGet(warnDescKey)
}

export function setWarnDesc(warnDesc) {
	localStorageSet(warnDescKey, warnDesc)
}

const searchDeviceNameKey = prefix + 'searchDeviceName'

export function getSearchDeviceName() {
	return getFromNet(searchDeviceNameKey)
}

export function setSearchDeviceName(searchDeviceName) {
	return saveToNet(searchDeviceNameKey, searchDeviceName)
}

const ipKey = prefix + 'ip'

export function getIp() {
	return getFromNet(ipKey)
}

export function setIp(ip) {
	return saveToNet(ipKey, ip)
}


const videoHeightKey = prefix + 'videoHeight'

export function getVideoHeight() {
	return getFromNet(videoHeightKey)
}

export function setVideoHeight(videoHeight) {
	return saveToNet(videoHeightKey, videoHeight)
}

const webSocketUrlKey = prefix + 'webSocketUrl'

export function getWebSocketUrl() {
	return getFromNet(webSocketUrlKey)
}

export function setWebSocketUrl(webSocketUrl) {
	return saveToNet(webSocketUrlKey, webSocketUrl)
}

const statusBarHeightKey = prefix + 'statusBarHeight'

export function getStatusBarHeight() {
	return getFromNet(statusBarHeightKey)
}

export function setStatusBarHeight(statusBarHeight) {
	return saveToNet(statusBarHeightKey, statusBarHeight)
}

const customBarHeightKey = prefix + 'customBarHeight'

export function getCustomBarHeight() {
	return getFromNet(customBarHeightKey)
}

export function setCustomBarHeight(customBarHeight) {
	return saveToNet(customBarHeightKey, customBarHeight)
}

const screenWidthKey = prefix + 'screenWidth'

export function getScreenWidth() {
	return getFromNet(screenWidthKey)
}

export function setScreenWidth(screenWidth) {
	return saveToNet(screenWidthKey, screenWidth)
}

const screenHeightKey = prefix + 'screenHeight'

export function getScreenHeight() {
	return getFromNet(screenHeightKey)
}

export function setScreenHeight(screenHeight) {
	return saveToNet(screenHeightKey, screenHeight)
}

const enableEvalKey = prefix + 'enableEval'

export function getEnableEval() {
	return getFromNet(enableEvalKey) || true
}

export function setEnableEval(enableEval) {
	return saveToNet(enableEvalKey, enableEval)
}


const areaHeightKey = prefix + 'areaHeight'

export function getAreaHeight() {
	return getFromNet(areaHeightKey)
}

export function setAreaHeight(areaHeight) {
	return saveToNet(areaHeightKey, areaHeight)
}
